import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;

public class MySort {
    /**
     * 插入排序
     * 时间复杂度：
     *         最坏情况下：逆序的时候 O(N^2)
     *         最好情况下：O(N)
     * 得出结论：当数据越有序  越快
     * 空间复杂度：O(1)
     * 稳定性：稳定的
     */
    public static void insertSort(int[] array){
        for (int i = 1; i <array.length ; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >=0 ; j--) {
                if (array[j]>tmp){
                    array[j+1]=array[j];
                }else {
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    /**
     * 希尔排序：
     * 时间复杂度：O(n^1.3   -  n^1.5)
     * 空间复杂度：O(1)
     * 不稳定的排序
     */
    public static void shellSort(int[] array){
        int gap=array.length;
        while (gap>1){
            gap/=2;
            shell(array,gap);
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i <array.length; i++) {
            int tmp=array[i];
            int j=i-gap;
            for (; j>=0; j-=gap) {
                if (array[j]>tmp){
                    array[j+gap]=array[j];
                }else {
                    break;
                }
            }
            array[j+gap]=tmp;
        }
    }
    /**
     * 选择排序 ：
     * 时间复杂度：O(N^2)
     *    没有最好情况 和 最坏情况
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     */
    public static void selectSort(int[] array){
        for (int i = 0; i < array.length ; i++) {
            int minIndex=i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j]< array[minIndex]){
                    minIndex=j;
                }
            }

            swap(array,i,minIndex);
        }
    }

    private static void swap(int[] array, int i, int j) {
        int tmp=array[i];
        array[i]= array[j];
        array[j]=tmp;
    }
    /**
     * 堆排序
     * 时间复杂度：O(n * logN)  对数据不敏感
     * 空间复杂度：O(1)
     * 稳定性：不稳定
     * */
    public static void heapSort(int[] array){
        createHeap(array);
        int end=array.length-1;
        while (end>0){
            swap(array,0,end);
            shiftDown(array,0,end);
            end--;
        }
    }
    private static void createHeap(int[] array){
        for (int parent = (array.length-1-1)/2; parent >=0 ; parent--) {
            shiftDown(array,parent,array.length);
        }
    }

    private static void shiftDown(int[] array, int parent, int length) {
        int child=2*parent+1;
        while (child<length){
            if (child+1<length&&array[child]<array[child+1]){
                child++;
            }
            if (array[child]>array[parent]){
                swap(array,child,parent);
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
    }
    //冒泡排序——稳定
    public static void bubbleSort(int[] array){
        for (int i = 0; i <array.length-1 ; i++) {
            boolean flag=false;
            for (int j = 0; j < array.length-1-i; j++) {
                if (array[j]>array[j+1]){
                    swap(array,j,j+1);
                    flag=true;
                }
            }
            if (!flag){
                break;
            }
        }
    }
    /*
    快速排序
    时间复杂度：最坏（有序/逆序）：O（n^2) 最好：O（N*logN）
    空间复杂度：最坏O（N） 最好：O（logN)
    稳定性：不稳定
    * */
    public static void quickSortDigPit(int[] array){
        quick(array,0,array.length-1);
    }
    private static void quickDigPit(int[] array, int start, int end) {
        if (start>=end){
            return;
        }
       // 三数取中的优化
        int index=midOfThree(array,start,end);
        swap(array,start,index);
        int dot=partitionDigPit(array,start,end);
        quick(array,start,dot-1);
        quick(array,dot+1,end);
    }

    private static int midOfThree(int[] array, int left, int right) {
        int mid=(left+right)/2;
        if (array[left]<array[right]){
            if (array[mid]<array[left]){
                return left;
            }else if (array[mid]>array[right]){
                return right;
            }else {
                return mid;
            }
        }else{
            if(array[mid]>array[left]){
                return left;
            }else if (array[mid]<array[right]){
                return right;
            }else {
                return mid;
            }
        }
    }

    private static int partitionDigPit(int[] array,int left,int right){
        int key=array[left];
        while (left<right){
            while (left<right&&array[right]>=key){
                right--;
            }
            array[left]=array[right];
            while (left<right&&array[left]<=key){
                left++;
            }
            array[right]=array[left];
        }
        array[left]=key;
        return left;
    }
    public static void quickSort(int[] array){
        quick(array,0,array.length-1);
    }

    private static void quick(int[] array, int start, int end) {
        if (start>=end){
            return;
        }
       int dot=partition(array,start,end);
        quick(array,start,dot-1);
        quick(array,dot+1,end);
    }

    private static int partition(int[] array, int left, int right) {
        int key=array[left];
        int i=left;
        while(left<right){
            while (left<right&&array[right]>=key){
                right--;
            }
            while (left<right&&array[left]<=key){
                left++;
            }
            swap(array,left,right);
        }
        swap(array,left,i);
        return left;
    }
    public static void insertQuickSort(int[] array,int left,int end){
        for (int i =left+1; i <end ; i++) {
            int tmp=array[i];
            int j = i-1;
            for (; j >=left ; j--) {
                if (array[j]>tmp){
                    array[j+1]=array[j];
                }else {
                    break;
                }
            }
            array[j+1]=tmp;
        }
    }
    public static void quickInsertSortDigPit(int[] array){
        quick(array,0,array.length-1);
    }
    private static void quickDigPitInsert(int[] array, int start, int end) {
        if (start>=end){
            return;
        }
         if (end-start+1<=15){
             insertQuickSort(array, start,end);
             return;
         }
        int dot=partitionDigPit(array,start,end);
        quick(array,start,dot-1);
        quick(array,dot+1,end);
    }

    public static void nonRecursiveSort(int[]array){
        int start=0;
        int end=array.length-1;
        int dot=partitionDigPit(array,start,end);
        Deque<Integer> stack=new LinkedList<>();
        if (dot>start+1){
            stack.push(start);
            stack.push(dot-1);
        }
        if (dot<end-1){
            stack.push(dot+1);
            stack.push(end);
        }
        while (!stack.isEmpty()){
            end=stack.pop();
            start= stack.pop();
            dot=partitionDigPit(array,start,end);
            if (dot>start+1){
                stack.push(start);
                stack.push(dot-1);
            }
            if (dot<end-1){
                stack.push(dot+1);
                stack.push(end);
            }
        }
    }
    //归并排序
    public static void mergeSort(int[] array){
        decompose(array,0,array.length-1);
    }

    private static void decompose(int[] array, int left, int right) {
        if (left==right){
            return;
        }
        int mid=(left+right)/2;
        decompose(array,left,mid);
        decompose(array,mid+1,right);
        combination(array,left,mid,right);

    }

    private static void combination(int[] array, int left, int mid, int right) {
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;
        int[] tempArray=new int[right-left+1];
        int k=0;
        while (s1<=e1&&s2<=e2){
           if (array[s1]<=array[s2]){
               tempArray[k++]=array[s1++];
           }else {
               tempArray[k++]=array[s2++];
           }
        }
        while (s1<=e1){
            tempArray[k++]=array[s1++];
        }
        while (s2<=e2){
            tempArray[k++]=array[s2++];
        }

        for (int i = 0; i <k ; i++) {
            array[i+left]=tempArray[i];
        }

    }
    public static void noRecursiveMergeSort(int[] array){
        int gap=1;
        while (gap<array.length){
            for (int i = 0; i <array.length; i=i+2*gap) {
                int left=i;
                int mid=left+gap-1;
                if (mid>=array.length){
                    mid=array.length-1;
                }
                int right=mid+gap;
                if (right>=array.length){
                    right=array.length-1;
                }
                combination(array,left,mid,right);
            }
            gap*=2;
        }
    }
    public static void countSort(int[] array){
        int max=array[0];
        int min=array[0];
        for (int i = 0; i <array.length ; i++) {
            if(array[i]>max){
                max=array[i];
            }
            if (array[i]<min){
                min=array[i];
            }
        }
        int[] countArray=new int[max-min+1];
        int k=0;
        for (int i = 0; i < array.length; i++) {
            int index=array[i]-min;
            countArray[index]++;
        }
        for (int i = 0; i <countArray.length ; i++) {
            while (countArray[i]!=0){
                array[k]=i+min;
                k++;
                countArray[i]--;
            }
        }

    }

}
